package com.ruoyi.project.system.order.service.impl;

import java.util.List;

import com.ruoyi.project.system.inventory.domain.ZzInventory;
import com.ruoyi.project.system.inventory.mapper.ZzInventoryMapper;
import com.ruoyi.project.system.item.domain.ZzCustomerOrderItem;
import com.ruoyi.project.system.item.mapper.ZzCustomerOrderItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.order.mapper.ZzCustomerOrderMapper;
import com.ruoyi.project.system.order.domain.ZzCustomerOrder;
import com.ruoyi.project.system.order.service.IZzCustomerOrderService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-10-28
 */
@Service
public class ZzCustomerOrderServiceImpl implements IZzCustomerOrderService 
{
    @Autowired
    private ZzCustomerOrderMapper zzCustomerOrderMapper;

    @Autowired
    private ZzInventoryMapper zzInventoryMapper;

    @Autowired
    private ZzCustomerOrderItemMapper zzCustomerOrderItemMapper;


    @Override
    public List<ZzCustomerOrder> findAllZzCustomerOrder() {
        return zzCustomerOrderMapper.seleceAllZzCustomerOrder();
    }

    @Override
    public ZzCustomerOrder findOneOrder(Integer customerOrderId) {
        System.out.println(zzCustomerOrderMapper.selectOneOrder(customerOrderId));
        return zzCustomerOrderMapper.selectOneOrder(customerOrderId);
    }

    //实现接单与判断是否货源充足的操作
    @Override
    public Integer changechangeOneOrder(ZzCustomerOrder zzCustomerOrder) {

        ZzCustomerOrderItem zzCustomerOrderItem = new ZzCustomerOrderItem();
        zzCustomerOrderItem.setCustomerOrderId(zzCustomerOrder.getCustomerOrderId());

        List<ZzCustomerOrderItem> zzCustomerOrderItems = zzCustomerOrderItemMapper.selectZzCustomerOrderItemList(zzCustomerOrderItem);

        for (ZzCustomerOrderItem zzCustomerOrderItem1:zzCustomerOrderItems) {
            ZzInventory zzInventory = new ZzInventory();
            zzInventory.setProductId(zzCustomerOrderItem1.getProductId());


            List<ZzInventory> zzInventories = zzInventoryMapper.selectZzInventoryList(zzInventory);
            for (ZzInventory zzInventory1:zzInventories) {
                if (zzInventory1.getInventoryProductNumber() < zzCustomerOrderItem1.getCustomerOrderItemNumber()) {
                    zzCustomerOrder.setCustomerOrderStatus("补货中");
                    return zzCustomerOrderMapper.updateZzCustomerOrder(zzCustomerOrder);
                }
            }
        }

        zzCustomerOrder.setCustomerOrderStatus("待出库");
        return zzCustomerOrderMapper.updateZzCustomerOrder(zzCustomerOrder);
    }


    /**
     * 查询【请填写功能名称】
     * 
     * @param customerOrderId 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public ZzCustomerOrder selectZzCustomerOrderById(Integer customerOrderId)
    {
        return zzCustomerOrderMapper.selectZzCustomerOrderById(customerOrderId);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param zzCustomerOrder 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ZzCustomerOrder> selectZzCustomerOrderList(ZzCustomerOrder zzCustomerOrder)
    {
        return zzCustomerOrderMapper.selectZzCustomerOrderList(zzCustomerOrder);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param zzCustomerOrder 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertZzCustomerOrder(ZzCustomerOrder zzCustomerOrder)
    {
        return zzCustomerOrderMapper.insertZzCustomerOrder(zzCustomerOrder);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param zzCustomerOrder 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateZzCustomerOrder(ZzCustomerOrder zzCustomerOrder)
    {
        return zzCustomerOrderMapper.updateZzCustomerOrder(zzCustomerOrder);
    }

    /**
     * 删除【请填写功能名称】对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteZzCustomerOrderByIds(String ids)
    {
        return zzCustomerOrderMapper.deleteZzCustomerOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param customerOrderId 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteZzCustomerOrderById(Integer customerOrderId)
    {
        return zzCustomerOrderMapper.deleteZzCustomerOrderById(customerOrderId);
    }
}
